package ru.nsu.fit.client_stub;

import ru.nsu.fit.shared.data_base_manager.DatabaseManager;
import ru.nsu.fit.ws.authentication.Authentication;
import ru.nsu.fit.ws.authentication.AuthenticationInput;
import ru.nsu.fit.ws.authentication.AuthenticationOutput;
import ru.nsu.fit.ws.get_questions.QuestionsGetter;
import ru.nsu.fit.ws.get_questions.QuestionsGetterInput;
import ru.nsu.fit.ws.get_questions.QuestionsGetterOutput;
import ru.nsu.fit.ws.get_questions_stat.QuestionsStatGetter;
import ru.nsu.fit.ws.get_questions_stat.QuestionsStatGetterInput;
import ru.nsu.fit.ws.get_questions_stat.QuestionsStatGetterOutput;
import ru.nsu.fit.ws.get_user_stat.UserStat;
import ru.nsu.fit.ws.get_user_stat.UserStatInput;
import ru.nsu.fit.ws.get_user_stat.UserStatOutput;
import ru.nsu.fit.ws.registration.Registration;
import ru.nsu.fit.ws.registration.RegistrationInput;
import ru.nsu.fit.ws.registration.RegistrationOutput;
import ru.nsu.fit.ws.send_answer.AnswerInput;
import ru.nsu.fit.ws.send_answer.AnswerOutput;
import ru.nsu.fit.ws.send_answer.AnswerSender;
import ru.nsu.fit.ws.send_question.QuestionInput;
import ru.nsu.fit.ws.send_question.QuestionOutput;
import ru.nsu.fit.ws.send_question.QuestionSender;
import ru.nsu.fit.ws.server_info.IServerInfo;
import ru.nsu.fit.ws.server_info.ServerInfoInputParameter;
import ru.nsu.fit.ws.server_info.ServerInfoOutputParameter;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.*;

/**
 * Created by IntelliJ IDEA.
 * User: vtimofeev
 * Date: 11/15/12
 * Time: 11:02 PM
 * To change this template use File | Settings | File Templates.
 */
public class Client {

    //This client-stub can be used for test purposes
    public static void main(String[] args) throws Exception {

//        testServerInfo();
//        registerUser("login2", "password2");
//          sendAnswer();
//        authenticate("jane", "jane");
//        printUserStat("joe", "joe");
        getQuestions();
//        getQuestStat();
//        sendQuestion();
    }

    private static void registerUser(String login, String password) throws MalformedURLException {

        URL url = new URL("http://localhost:8080/ws/registration?wsdl");
        QName qname = new QName("http://registration.ws.fit.nsu.ru/", "RegistrationImplService");

        Service service = Service.create(url, qname);
        Registration registration = service.getPort(Registration.class);

        RegistrationInput input = new RegistrationInput();
        input.setLogin(login);
        input.setPassword(password);

        System.out.println("Trying to register user "+login + " with password " + password);
        RegistrationOutput output = registration.registerUser(input);

        System.out.println("Registration was successful: "+output.isSuccessful());
        System.out.println("Error message: "+output.getErrorMessage());
    }

    private static long authenticate(final String login, final String password) throws MalformedURLException {
        final URL url = new URL("http://localhost:8080/ws/authentication?wsdl");
        QName qname = new QName("http://authentication.ws.fit.nsu.ru/", "AuthenticationImplService");

        final Service service = Service.create(url, qname);
        final Authentication authentication = service.getPort(Authentication.class);

        final AuthenticationInput input = new AuthenticationInput();
        input.setLogin(login);
        input.setPassword(password);

        System.out.println("Trying to authenticate user "+login + " with password " + password);
        final AuthenticationOutput output = authentication.authenticate(input);

        System.out.println("Id: " + output.getUserSessionId());
        System.out.println("Error message: " + output.getErrorMessage());
        return output.getUserSessionId();
    }

    private static void printUserStat(final String login, final String password) throws MalformedURLException {
        final URL url = new URL("http://localhost:8080/ws/get_user_stat?wsdl");
        final QName qName = new QName("http://get_user_stat.ws.fit.nsu.ru/", "UserStatImplService");
        final Service service = Service.create(url, qName);
        final UserStat stat = service.getPort(UserStat.class);
        final long userSessionId = authenticate(login, password);
        final UserStatInput input = new UserStatInput();
        input.setUserSessionId(userSessionId);
        final UserStatOutput output = stat.getUserStat(input);
        System.out.println("Is successful: " + output.isSuccessful());
        System.out.println("Error message: " + output.getErrorMessage());
        System.out.println("User rating: " + output.getUserRating());
//        System.out.println("User rating position: " + output.getUserRatingPosition());
//        System.out.println("Asked questions count: " + output.getAskedQuestionsCount());
//        System.out.println("Answered questions count: " + output.getAnsweredQuestionsCount());
    }

    private static void testServerInfo() throws MalformedURLException {

        URL url = new URL("http://localhost:8080/ws/server_info?wsdl");
        QName qname = new QName("http://server_info.ws.fit.nsu.ru/", "ServerInfoImplService");

        Service service = Service.create(url, qname);
        IServerInfo info = service.getPort(IServerInfo.class);

        ServerInfoOutputParameter out = info.getServerInfo(new ServerInfoInputParameter());
        System.out.println("Server info:");
        System.out.println(out.getInfo() + " " + out.getVersion() + " "+ out.getDate());
    }

    private static void sendQuestion() throws MalformedURLException {

        URL url = new URL("http://localhost:8080/ws/send_question?wsdl");
        QName qname = new QName("http://send_question.ws.fit.nsu.ru/",
                "QuestionSenderImplService");

        Service service = Service.create(url, qname);
        QuestionSender questionSender = service.getPort(QuestionSender.class);

        QuestionInput in = new QuestionInput();
        in.setUserSessionId(-616644751);
        in.setQuestionText("WTF?");

        QuestionOutput out = questionSender.askQuestion(in);
        System.out.println("Question asked. Id: "+out.getQuestionId());
        System.out.println("Error code: "+out.getErrorCode());
        System.out.println("Error mes: "+out.getErrorMessage());
    }

    private static void sendAnswer() throws MalformedURLException {

        URL url = new URL("http://localhost:8080/ws/send_answer?wsdl");
        QName qname = new QName("http://send_answer.ws.fit.nsu.ru/",
                "AnswerSenderImplService");

        Service service = Service.create(url, qname);
        AnswerSender answerSender = service.getPort(AnswerSender.class);

        AnswerInput in = new AnswerInput();
        in.setUserSessionId(-984827506);
        in.setQuestionId(18);
        in.setAnswerId(24);

        AnswerOutput out = answerSender.sendAnswer(in);
        System.out.println("Answer send. Success: "+out.isSuccessful());
        System.out.println("Error code: "+out.getErrorCode());
        System.out.println("Error mes: "+out.getErrorMessage());
    }

    private static void getQuestions() throws MalformedURLException {
        URL url = new URL("http://localhost:8080/ws/get_questions?wsdl");
        QName qname = new QName("http://get_questions.ws.fit.nsu.ru/",
                "QuestionsGetterImplService");

        Service service = Service.create(url, qname);
        QuestionsGetter questionsGetter = service.getPort(QuestionsGetter.class);

        QuestionsGetterInput  in = new QuestionsGetterInput();
        in.setUserSessionId(-1643597676);
        in.setQuestionsOffset(0);
        in.setQuestionNumber(3);
        in.setLatitude(0.0d);
        in.setLongitude(0.0d);
        in.setRadius(0.0d);

        QuestionsGetterOutput out = questionsGetter.getQuestions(in);
        for (int i = 0; i < out.getQuestionIds().length; ++i) {
            System.out.println(out.getQuestionTexts()[i] + " " + out.getQuestionIds()[i]);
            for (int j = 0; j < out.getAnswerIds()[i].length; ++j) {
                System.out.println(out.getAnswers()[i][j] + " " + out.getAnswerIds()[i][j]);
            }
            System.out.println();
        }
        System.out.println("Error code: "+out.getErrorCode());
        System.out.println("Error mes: "+out.getErrorMessage());
    }

    private static void getQuestStat() throws MalformedURLException {
        URL url = new URL("http://localhost:8080/ws/get_questions_stat?wsdl");
        QName qname = new QName("http://get_questions_stat.ws.fit.nsu.ru/",
                "QuestionsStatGetterImplService");

        Service service = Service.create(url, qname);
        QuestionsStatGetter questionsGetter = service.getPort(QuestionsStatGetter.class);

        QuestionsStatGetterInput in = new QuestionsStatGetterInput();
        in.setUserSessionId(-984827506);
        in.setQuestionIds(new int[]{17,18});

        QuestionsStatGetterOutput out = questionsGetter.getQuestionsStat(in);

        System.out.println("Error code: "+out.getErrorCode());
        System.out.println("Error mes: "+out.getErrorMessage());
    }
}
